Completed
Pull Request — master (#76)
by Maxence
03:22
created

actions.createCircleResult   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
cc 3
nc 3
nop 1
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
/** global: actions */
31
/** global: nav */
32
/** global: elements */
33
/** global: curr */
34
/** global: api */
35
36
37
var actions = {
38
39
40
	joinCircleResult: function (result) {
41
		if (result.status === 0) {
42
			OCA.notification.onFail(
43
				t('circles', "Cannot join this circle") + ': ' +
44
				((result.error) ? result.error : t('circles', 'no error message')));
45
			return;
46
		}
47
48
		elements.removeMemberslistEntry(result.member.user_id);
49
		if (result.member.level === define.levelMember) {
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
50
			OCA.notification.onSuccess(
51
				t('circles', "You have successfully joined this circle"));
52
		} else {
53
			OCA.notification.onSuccess(
54
				t('circles', "You have requested to join this circle"));
55
		}
56
		actions.selectCircle(result.circle_id);
57
	},
58
59
60
	leaveCircleResult: function (result) {
61
		if (result.status === 1) {
62
63
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
64
				function () {
65
					$(this).hide(300);
66
				});
67
68
			actions.selectCircle(result.circle_id);
69
			OCA.notification.onSuccess(
70
				t('circles', "You have successfully left this circle"));
71
			return;
72
		}
73
74
		OCA.notification.onFail(
75
			t('circles', "Cannot leave this circle") + ': ' +
76
			((result.error) ? result.error : t('circles', 'no error message')));
77
	},
78
79
80
	destroyCircleResult: function (result) {
81
		if (result.status === 1) {
82
83
			actions.unselectCircle(result.circle_id);
84
			OCA.notification.onSuccess(
85
				t('circles', "You have successfully deleted this circle"));
86
			return;
87
		}
88
89
		OCA.notification.onFail(
90
			t('circles', "Cannot delete this circle") + ': ' +
91
			((result.error) ? result.error : t('circles', 'no error message')));
92
	},
93
94
95
	createCircleResult: function (result) {
96
		var type = actions.getStringTypeFromType(result.type);
97
98
		if (result.status === 1) {
99
			OCA.notification.onSuccess(t('circles', " {type} '{name}' created", {
100
				type: type,
101
				name: result.name
102
			}));
103
			elements.emptyCircleCreation();
104
			nav.displayCirclesList(result.circle.type);
105
			actions.selectCircle(result.circle.id);
106
			return;
107
		}
108
109
		OCA.notification.onFail(
110
			t('circles', " {type} '{name}' could not be created", {
111
				type: type,
112
				name: result.name
113
			}) + ': ' +
114
			((result.error) ? result.error : t('circles', 'no error message')));
115
	},
116
117
118
	selectCircleResult: function (result) {
119
120
		elements.mainUIMembers.emptyTable();
121
		if (result.status < 1) {
122
			OCA.notification.onFail(
123
				t('circles', 'Issue while retrieving the details of this circle') + '" ' +
124
				((result.error) ? result.error : t('circles', 'no error message')));
125
			return;
126
		}
127
128
		elements.navigation.children('.circle').removeClass('selected');
129
		elements.navigation.children(".circle[circle-id='" + result.circle_id + "']").each(
130
			function () {
131
				$(this).addClass('selected');
132
			});
133
134
		elements.emptyContent.hide(800);
135
		elements.mainUI.fadeIn(800);
136
		curr.circle = result.circle_id;
137
		curr.circleLevel = result.details.user.level;
138
		curr.circleStatus = result.details.user.status;
139
140
		nav.displayCircleDetails(result.details);
141
		nav.displayMembersInteraction(result.details);
142
		nav.displayMembers(result.details.members);
143
	},
144
145
146
	listCirclesResult: function (result) {
147
148
		if (result.status < 1) {
149
			OCA.notification.onFail(
150
				t('circles', 'Issue while retrieving the list of circles') + '; ' +
151
				((result.error) ? result.error : t('circles', 'no error message')));
152
			return;
153
		}
154
155
		elements.resetCirclesList();
156
157
		var data = result.data;
158
		for (var i = 0; i < data.length; i++) {
159
			var tmpl = elements.generateTmplCircle(data[i]);
160
			elements.navigation.append(
161
				'<div class="circle" circle-id="' + data[i].id + '">' + tmpl + '</div>');
162
		}
163
164
		elements.navigation.children('.circle').on('click', function () {
165
			actions.selectCircle($(this).attr('circle-id'));
166
		});
167
	},
168
169
	linkCircleResult: function (result) {
170
171
		//elements.linkCircle.val('');
172
		if (result.status !== 1) {
173
			OCA.notification.onFail(
174
				t('circles', "A link to <b>{remote}</b> could not be initiated", {
175
					remote: result.remote
176
				}) + ': ' +
177
				((result.error) ? result.error : t('circles', 'no error message')));
178
			return;
179
		}
180
181
		if (result.link.status === define.linkRequested) {
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
182
			OCA.notification.onSuccess(
183
				t('circles', "A link to <b>{remote}</b> has been requested.", {
184
					remote: result.remote
185
				}));
186
		}
187
188
		if (result.link.status === define.linkUp) {
189
			OCA.notification.onSuccess(
190
				t('circles', "the link to <b>{remote}</b> is now up and running.", {
191
					remote: result.remote
192
				}));
193
		}
194
195
	},
196
197
198
	changeMemberLevel: function (member, level) {
199
		api.levelMember(curr.circle, member, level, actions.levelMemberResult);
200
		nav.circlesActionReturn();
201
	},
202
203
204
	changeMemberStatus: function (member, value) {
205
		if (value === 'remove_member' || value === 'dismiss_request') {
206
			api.removeMember(curr.circle, member, actions.removeMemberResult);
207
		}
208
		if (value === 'accept_request') {
209
			api.addMember(curr.circle, member, actions.addMemberResult);
210
		}
211
	},
212
213
214
	selectCircle: function (circle_id) {
215
		curr.searchUser = '';
216
		elements.addMember.val('');
217
		elements.linkCircle.val('');
218
219
		nav.circlesActionReturn();
220
		api.detailsCircle(circle_id, actions.selectCircleResult);
221
	},
222
223
224
	unselectCircle: function (circle_id) {
225
		elements.mainUIMembers.emptyTable();
226
		elements.navigation.children(".circle[circle-id='" + circle_id + "']").remove();
227
		elements.emptyContent.show(800);
228
		elements.mainUI.fadeOut(800);
229
230
		curr.circle = 0;
231
		curr.circleLevel = 0;
232
	},
233
234
235
	/**
236
	 *
237
	 * @param search
238
	 */
239
	searchMembersRequest: function (search) {
240
241
		if (curr.searchUser === search) {
242
			return;
243
		}
244
245
		curr.searchUser = search;
246
247
		$.get(OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees',
248
			{
249
				format: 'json',
250
				search: search,
251
				perPage: 200,
252
				itemType: 'principals'
253
			}, actions.searchMembersResult);
254
	},
255
256
257
	searchMembersResult: function (response) {
258
259
		elements.membersSearchResult.children().remove();
260
261
		if (response === null ||
262
			(response.ocs.data.users.length === 0 && response.ocs.data.exact.users.length === 0)) {
263
			elements.membersSearchResult.fadeOut(0);
264
			return;
265
		}
266
267
		elements.fillMembersSearch(response.ocs.data.exact.users, response.ocs.data.users);
268
269
		$('.members_search').on('click', function () {
270
			api.addMember(curr.circle, $(this).attr('searchresult'),
271
				actions.addMemberResult);
272
		});
273
		elements.membersSearchResult.fadeIn(300);
274
	},
275
276
277
	addMemberResult: function (result) {
278
279
		if (result.status === 1) {
280
			OCA.notification.onSuccess(
281
				t('circles', "Member '{name}' successfully added to the circle",
282
					{name: result.name}));
283
284
			nav.displayMembers(result.members);
285
			return;
286
		}
287
		OCA.notification.onFail(
288
			t('circles', "Member '{name}' could not be added to the circle", {name: result.name}) +
289
			': ' +
290
			((result.error) ? result.error : t('circles', 'no error message')));
291
	},
292
293
294
	getStringTypeFromType: function (type) {
295
		switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
296
			case '1':
297
				return t('circles', 'Personal circle');
298
			case '2':
299
				return t('circles', 'Hidden circle');
300
			case '4':
301
				return t('circles', 'Private circle');
302
			case '8':
303
				return t('circles', 'Public circle');
304
		}
305
306
		return t('circles', 'Circle');
307
	},
308
309
310
	removeMemberResult: function (result) {
311
		if (result.status === 1) {
312
313
			elements.rightPanel.fadeOut(300);
314
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
315
				function () {
316
					$(this).hide(300);
317
				});
318
			OCA.notification.onSuccess(
319
				t('circles', "Member '{name}' successfully removed from the circle",
320
					{name: result.name}));
321
			return;
322
		}
323
324
		OCA.notification.onFail(
325
			t('circles', "Member '{name}' could not be removed from the circle",
326
				{name: result.name}) +
327
			': ' +
328
			((result.error) ? result.error : t('circles', 'no error message')));
329
	},
330
331
	levelMemberResult: function (result) {
332
		if (result.status === 1) {
333
			OCA.notification.onSuccess(
334
				t('circles', "Member '{name}' updated",
335
					{name: result.name}));
336
337
			nav.displayMembers(result.members);
338
			return;
339
		}
340
		OCA.notification.onFail(
341
			t('circles', "Member '{name}' could not be updated", {name: result.name}) +
342
			': ' +
343
			((result.error) ? result.error : t('circles', 'no error message')));
344
	},
345
346
347
	/**
348
	 *
349
	 */
350
	onEventNewCircle: function () {
351
		curr.circle = 0;
352
		curr.circleLevel = 0;
353
354
		elements.circlesList.children('div').removeClass('selected');
355
		elements.emptyContent.show(800);
356
		elements.mainUI.fadeOut(800);
357
	},
358
359
360
	/**
361
	 *
362
	 */
363
	onEventNewCircleName: function () {
364
		this.onEventNewCircle();
365
		nav.displayOptionsNewCircle((elements.newName.val() !== ''));
366
	},
367
368
369
	/**
370
	 *
371
	 */
372
	onEventNewCircleType: function () {
373
		this.onEventNewCircle();
374
		elements.newTypeDefinition.children('div').fadeOut(300);
375
		var selectedType = elements.newType.children('option:selected').val();
376
		if (selectedType === '') {
377
			elements.newType.addClass('select_none');
378
		}
379
		else {
380
			elements.newType.removeClass('select_none');
381
			$('#circles_new_type_' + selectedType).fadeIn(
382
				300);
383
		}
384
	}
385
386
387
};
388